package com.jdyfy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdyfy.dto.HistoTaskDTO;
import com.jdyfy.dto.StatisticsDTO;
import com.jdyfy.entity.Project;
import com.jdyfy.mapper.ProjectMapper;
import com.jdyfy.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    //分页获取项目数据
    public List<Project> getProjectPage(Integer pageNum, Integer pageSize, String projectNumber){
        return projectMapper.getProjectPage(pageNum,pageSize,projectNumber);
    }

    public Project selectByNumber(String projectNumber){
        return projectMapper.selectByNumber(projectNumber);
    }

    public List<HistoTaskDTO> getHistoProject(int status) {
        return projectMapper.getHistoProject(status);
    }

    //通过id删除
    //public Long removeById(Long projectId){
    //    return removeById(projectId);
    //}

    //批量删除
    @Override
    public int delByIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return 0;
        }
        return projectMapper.delByIds(ids);
    }

    //获取科室项目柱状图数据
    public StatisticsDTO getSPBarCharts() {

        // 获取所有项目数据
        List<Project> projects = list();

        // 使用 Map 存储统计信息
        Map<String, Integer> adCountMap = new HashMap<>();

        // 遍历项目数据，统计每个科室的项目数量
        for (Project project : projects) {
            String ad = project.getApplyingDepartment();
            adCountMap.put(ad, adCountMap.getOrDefault(ad, 0) + 1);
        }

        // 将 Map 的 Key 和 Value 转换成数组
        List<String> adList = new ArrayList<>(adCountMap.keySet());
        List<Integer> countList = new ArrayList<>(adCountMap.values());

        // 将 List 转换成数组
        String[] adArray = adList.toArray(new String[0]);
        Integer[] countArray = countList.toArray(new Integer[0]);

        // 创建并返回 StatusStatisticsDTO 对象
        StatisticsDTO dto = new StatisticsDTO();
        dto.setAppArray(adArray);
        dto.setCountArray(countArray);

        return dto;
    }

    //获取项目类型饼图数据
    public Map<String, Integer> getPTCharts() {

        // 获取所有项目数据
        List<Project> projects = list();

        // 使用 Map 存储统计信息
        Map<String, Integer> ptCountMap = new HashMap<>();

        // 遍历项目数据，统计每个科室的项目数量
        for (Project project : projects) {
            String pt = project.getProjectType();
            ptCountMap.put(pt, ptCountMap.getOrDefault(pt, 0) + 1);
        }

        return ptCountMap;
    }

    //获取采购方式饼图数据
    public Map<String, Integer> getPMCharts() {

        // 获取所有项目数据
        List<Project> projects = list();

        // 使用 Map 存储统计信息
        Map<String, Integer> pmCountMap = new HashMap<>();

        // 遍历项目数据，统计每个科室的项目数量
        for (Project project : projects) {
            String pm = project.getProcurementMethod();
            pmCountMap.put(pm, pmCountMap.getOrDefault(pm, 0) + 1);
        }

        return pmCountMap;
    }

    // 通过状态获取所有项目
    public List<Project> getProjectsByStatus(Integer annoStatusStart, Integer annoStatusEnd) {
        return projectMapper.getProjectsByStatus(annoStatusStart,annoStatusEnd);
    }


}
